package com.nwork.cateye.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.nwork.cateye.R;
import com.nwork.cateye.model.MovieModel;
import com.nwork.cateye.utils.CommonUtils;

import java.util.List;

/**
 * Created by Administrator on 2015/3/25.
 */
public class MovieZhengListViewAdapter extends BaseAdapter {
    private Context context;
    private ViewPager imgPager;
    private List<MovieModel> movieModels;
    private BitmapUtils bitmapUtils;

    public MovieZhengListViewAdapter(Context context, List<MovieModel> movieModels, BitmapUtils bitmapUtils) {
        this.context = context;
        this.movieModels = movieModels;
        this.bitmapUtils = bitmapUtils;
    }

    @Override
    public int getCount() {
        return movieModels.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        if (position == 0) {
            return 0;
        }
        return 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        ViewHolder viewHolder = null;
        LayoutInflater inflater = LayoutInflater.from(context);
        if (convertView != null) {
            ret = convertView;
        } else {
            if (position == 0) {
                ret = inflater.inflate(R.layout.item_movie_fragment_top_image, parent, false);
                imgPager = (ViewPager) convertView.findViewById(R.id.img_viewpager);
                imgPager.setCurrentItem(500);
//                imgPager.setAdapter(new ImageViewPagerAdapter());
//                imgPager.setAdapter(new ImageViewPagerAdapter(context));
            } else {
                ret = inflater.inflate(R.layout.item_movie_fragment_zheng, parent, false);
            }
        }
        if (ret != null) {
            if (position == 0) {

            } else {
                initView(ret, movieModels.get(position));
            }
        }
        return ret;
    }


    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        int ret = 0;
        if (position == 0) {
            ret = 0;
        } else {
            ret = 1;
        }
        return ret;
    }

    public static class ViewHolder {
        public ImageView movieImage;
        public TextView movieName;
        public TextView movieCinemaNumber;
        public TextView movieTicket;
        public TextView movieDescription;
        public TextView moviePeople;
        public ImageView movieNew;
        public ImageView movieMax;
    }

    private void initView(View view, MovieModel movieModel) {
        if (view != null && movieModel != null) {
            ViewHolder viewHolder = (ViewHolder) view.getTag();
            if (viewHolder == null) {
                viewHolder = new ViewHolder();
                viewHolder.movieCinemaNumber = (TextView) view.findViewById(R.id.movie_cinema_number);
                viewHolder.movieName = (TextView) view.findViewById(R.id.movie_name);
                viewHolder.movieImage = (ImageView) view.findViewById(R.id.movie_img);
                viewHolder.movieTicket = (TextView) view.findViewById(R.id.movie_ticket);
                viewHolder.movieDescription = (TextView) view.findViewById(R.id.movie_description);
                viewHolder.moviePeople = (TextView) view.findViewById(R.id.movie_people);
                viewHolder.movieNew = (ImageView) view.findViewById(R.id.movie_new);
                viewHolder.movieMax = (ImageView) view.findViewById(R.id.movie_max);
                view.setTag(viewHolder);
            }
            viewHolder.movieCinemaNumber.setText(movieModel.getDesc());
            viewHolder.moviePeople.setText(movieModel.getSc() + "");
            viewHolder.movieName.setText(movieModel.getNm());
            viewHolder.movieDescription.setText(movieModel.getScm());
            String img = movieModel.getImg();
            String imageUrl = CommonUtils.getImageUrl(img);
            bitmapUtils.display(viewHolder.movieImage, imageUrl);
        }

    }

}
